<{include file="header.html"}>

<script type="text/javascript">

	function validate()
	{
		var name = document.getElementById('NAME');
		var pos = document.getElementById('POS');
		var image = document.getElementById('IMAGE');
		var short_description = document.getElementsByName('SHORT_DESCRIPTION')[0];
		var description = document.getElementsByName('DESCRIPTION')[0];

		// update content of CKEDITOR to textareas
		CKEDITOR.instances.DESCRIPTION.updateElement();

		// checking NAME
		if(isEmptyTextBox(name, '<{$smarty.const._MSG_NAME_IS_EMPTY|utf8_escape_js}>'))
			return false;

		if(!isValidLengthTextBox(name, <{$smarty.const.MAX_LENGTH_NAME}>, '<{$smarty.const._MSG_NAME_IS_TOO_LONG|utf8_escape_js}>')){
            return false;
        }

		if (isExistProduct(name.value)) {
			showErrorBubble(name, '<{$smarty.const._MSG_NAME_IS_EXIST|utf8_escape_js}>');
			return false;
		}

		// checking POS
		if (!isPositiveWholeInteger(pos.value)) {
			showErrorBubble(pos, '<{$smarty.const._MSG_POSITION_IS_INVALID|utf8_escape_js}>');
			return false;
		}

		// validate IMAGE
		// - IMAGE must be not empty
		// - the extension must is 'png','jpg','jpeg','gif', or 'bmp'
		<{if $product->ID}>
		if ($('#IMAGE').val() != '' && !validateExtension($('#IMAGE').val(), new Array('png','jpg','jpeg','gif','bmp'))) {
			showErrorBubble($('#IMAGE'), '<{$smarty.const._MSG_IMAGE_EXTENSION_IS_INVALID|utf8_escape_js}>');
			return false;
		}
		<{else}>
		if(isEmptyTextBox(image, '<{$smarty.const._MSG_IMAGE_IS_EMPTY|utf8_escape_js}>')) {
			return false;
		}
		if (!$('#IMAGE').val() || $('#IMAGE').val() == '' || !validateExtension($('#IMAGE').val(), new Array('png','jpg','jpeg','gif','bmp'))) {
			showErrorBubble($('#IMAGE'), '<{$smarty.const._MSG_IMAGE_EXTENSION_IS_INVALID|utf8_escape_js}>111');
			return false;
		}
		<{/if}>

		// checking SHORT_DESCRIPTION
        if(isEmptyTextBox(short_description, '<{$smarty.const._MSG_SHORT_DESCRIPTION_IS_EMPTY|utf8_escape_js}>'))
			return false;
		
		// checking DESCRIPTION
        if(isEmptyTextBox(description, '<{$smarty.const._MSG_DESCRIPTION_IS_EMPTY|utf8_escape_js}>')) {
			return false;
		}

		var product_images = document.getElementsByName('PRODUCT_IMAGE[]');
		var product_image_ids = document.getElementsByName('PRODUCT_IMAGE_ID[]');
		for (var i = 0; i < product_images.length-1; i++) {
			if (product_images[i].value != '' && !validateExtension(product_images[i].value, new Array('png','jpg','jpeg','gif','bmp'))) {
				showErrorBubble(product_images[i], '<{$smarty.const._MSG_IMAGE_EXTENSION_IS_INVALID|utf8_escape_js}>222');
				return false;
			}
		}

		$('.display-checkbox').each(function(){
			$(this).parent().find('>.hide-checkbox').val($(this).attr('checked') ? 1 : 0);
		});

		document.user_form.submit();
	}

	function validateExtension(fileName, extension)
	{
		var fileExtension = getFileExtension(fileName).toLowerCase();
		if ($.isArray(extension)) {
			for(var i = 0; i < extension.length; i++) {
				if (fileExtension == extension[i].toLowerCase()) {
					return true;
				}
			}
		} else {
			return (fileExtension == extension.toLowerCase());
		}

		return false;
	}

	function getFileExtension(fileName)
	{
		var found = fileName.lastIndexOf('.') + 1;
		return (found > 0 ? fileName.substr(found) : "");
	}

	function isExistProduct(name) {
		var args = Array();
		var id_product = <{if ($product->ID)}><{$product->ID}><{else}>0<{/if}>;
		args.push(name);
		args.push(id_product);

		var ajaxRes = callFunction('Product::isExist', args);

		return (ajaxRes == 0 ? false : true);
	}

	$(document).ready(function() {
		$('.remove-item').live("click", function(){
			if ($(this).parent().find('>.item-id').val()) {
				$(this).parent().find('>.item-removed').val(1);
				$(this).parent().hide();
			} else {
				$(this).parent().remove();
			}
		});
		$('.add-item').live("click", function(){
			var dynamic_list = $(this).parent();
			var item_html = dynamic_list.find('> .sample-item').html();
			if (dynamic_list.find('.auto_increase')) {
				var id = parseInt('0' + dynamic_list.find('.auto_increase').val()) + 1;
				dynamic_list.find('.auto_increase').val(id);
				item_html = item_html.replace(/PRODUCT_INFO_CONTENT_AUTO/g,"PRODUCT_INFO_CONTENT_AUTO_" + id);
			}
			dynamic_list.find('> .list-item').append(item_html);
			if (dynamic_list.find('.auto_increase')) {
				CKEDITOR.replace("PRODUCT_INFO_CONTENT_AUTO_" + id);
			}
		});
		$('.select_type').live('change', function(){
			var table = $(this).parent().parent().parent();
			if ($(this).val() == '0') {
				table.find('>.rw_file_upload').show();
				table.find('>.rw_url').hide();
			} else {
				table.find('>.rw_file_upload').hide();
				table.find('>.rw_url').show();
			}
		});
		$('.select_type').change();		
	});

</script>

		<div class="box_two_third">
			<div class="box_head"><{$smarty.const._LB_PRODUCT_MANAGEMENT|utf8_escape_html}></div>
			<div class="box_content">

				<form enctype="multipart/form-data" action="<{$smarty.server.REQUEST_URI}>" method="post" name="user_form">
				<input type="hidden" id="ID" value="<{$product->ID}>"/>
				<input type="hidden" id="act" name="act" value="<{$smarty.const.ACT_VALID_EDIT}>"/>
				<table>
                    <tr>
						<td width="8%"><{$smarty.const._LB_NAME|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
						<td width="60%"><input name="NAME" id="NAME" type="text" size="50" value="<{$product->NAME|utf8_escape_html}>" /></td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td ><{$smarty.const._LB_CATEGORY|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
                        <td colspan="3">
                            <select name="ID_CATEGORY" id="ID_CATEGORY">
                                <option value="0">-- Root --</option>
                                <{include file="category_tree_select_parent.html" cats=$cats padding=0 id_current=0 id_parent=$product->ID_CATEGORY}>
                            </select>
                        </td>
					</tr>
                    <tr>
						<td ><{$smarty.const._LB_POS|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
						<td colspan="3"><input name="POS" id="POS" type="text" size="2" maxlength="2" value="<{$product->POS|utf8_escape_html}>" /></td>
					</tr>
					<tr>
						<td><{$smarty.const._LB_IMAGE|utf8_escape_html}>&nbsp:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
						<td>
							<{if $product->IMAGE}>
								<img src="<{$smarty.const.PRODUCT_IMAGE_URL}>/thumb<{$product->IMAGE}>"><br/>
							<{/if}>
							<input type="file" name="IMAGE" id="IMAGE" size="60" />
						</td>						
					</tr>
					<tr>
						<td ><{$smarty.const._LB_SHORT_DESCRIPTION|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
                        <td colspan="3"><textarea name="SHORT_DESCRIPTION" id="SHORT_DESCRIPTION" cols="60" rows="6" ><{$product->SHORT_DESCRIPTION|utf8_escape_html}></textarea></td>
					</tr>
					<tr>
						<td ><{$smarty.const._LB_DESCRIPTION|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
						<td colspan="3">
                            <{$CKEditor->editor("DESCRIPTION",$product->DESCRIPTION)}>
                        </td>
					</tr>
					
                    <tr>
						<td ><{$smarty.const._LB_DISABLED|utf8_escape_html}>&nbsp;:&nbsp;</td>
						<td colspan="3"><input type="checkbox" value="1" name="DISABLED" <{if $product->DISABLED}>checked<{/if}>/></td>
					</tr>
				</table>
				<div class="spacer"></div>
				<div class="section">
					<div class="section_title"><{$smarty.const._LB_PRODUCT_INFO|utf8_escape_html}></div>
					<div class="section_content">
						<div class="dynamic-list" style="padding-top: 10px;">
							<div class ="list-item">
							<{foreach key=key item=product_info from=$product->product_infos}>
								<div class="item-content">
									<input class="item-id" name="PRODUCT_INFO_ID[]" type="hidden" value="<{$product_info->ID}>"/>
									<input class="item-removed" name="PRODUCT_INFO_REMOVED[]" type="hidden" value="0"/>
									<table>
										<tr>
											<td width="8%"><{$smarty.const._LB_NAME|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td width="60%"><input name="PRODUCT_INFO_NAME[]" type="text" size="50" value="<{$product_info->NAME|utf8_escape_html}>" /></td>
											<td>&nbsp;</td>												
										</tr>
										<tr>
											<td ><{$smarty.const._LB_CONTENT|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td colspan="3">
												<{$CKEditor->editor("PRODUCT_INFO_CONTENT_ID_`$product_info->ID`",$product_info->CONTENT)}>
												<input class="hide-editor" type="hidden" value="<{if $product_info->ID}>PRODUCT_INFO_CONTENT_ID_<{$product_info->ID}><{else}><{$product_info->CONTENT_NAME}><{/if}>" name="PRODUCT_INFO_CONTENT_NAME[]" />
											</td>
										</tr>
										<tr>
											<td><{$smarty.const._LB_DISABLED|utf8_escape_html}>&nbsp;:&nbsp;</td>
											<td>
												<input class="display-checkbox" type="checkbox" value="1" <{if $product_info->DISABLED}>checked<{/if}> />
												<input class="hide-checkbox" type="hidden" value="" name="PRODUCT_INFO_DISABLED[]" />
											</td>
										</tr>
									</table>
									<span class="remove-item link" style="padding-left: 2px;"><{$smarty.const._LB_REMOVE|utf8_escape_html}></span>
									<hr/>
								</div>
							<{/foreach}>
							</div>
							<span class="add-item link" style="padding-top:10px;">
								Add more info
							</span>
							<input class="auto_increase" type="hidden" value="<{$auto_increase}>" name="auto_increase"/>
							<div class="sample-item" style="display:none;">
								<div class="item-content">
									<input class="item-id" name="PRODUCT_INFO_ID[]" type="hidden"/>
									<input class="item-removed" name="PRODUCT_INFO_REMOVED[]" type="hidden" value="0"/>
									<table>
										<tr>
											<td width="8%"><{$smarty.const._LB_NAME|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td width="60%"><input name="PRODUCT_INFO_NAME[]" type="text" size="50" value="" /></td>
											<td>&nbsp;</td>												
										</tr>
										<tr>
											<td ><{$smarty.const._LB_CONTENT|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td colspan="3">
												<textarea id="PRODUCT_INFO_CONTENT_AUTO" name="PRODUCT_INFO_CONTENT_AUTO"></textarea>
												<input class="hide-editor" type="hidden" value="PRODUCT_INFO_CONTENT_AUTO" name="PRODUCT_INFO_CONTENT_NAME[]" />
											</td>
										</tr>
										<tr>
											<td><{$smarty.const._LB_DISABLED|utf8_escape_html}>&nbsp;:&nbsp;</td>
											<td>
												<input class="display-checkbox" type="checkbox" value="1" />
												<input class="hide-checkbox" type="hidden" value="" name="PRODUCT_INFO_DISABLED[]" />
											</td>
										</tr>
									</table>
									<span class="remove-item link" style="padding-left: 2px;"><{$smarty.const._LB_REMOVE|utf8_escape_html}></span>
									<hr/>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="spacer"></div>
				<div class="section">
					<div class="section_title"><{$smarty.const._LB_PRODUCT_IMAGE|utf8_escape_html}></div>
					<div class="section_content">
						<div class="dynamic-list" style="padding-top: 10px;">
							<div class ="list-item">
							<{foreach key=key item=product_image from=$product->product_images}>
								<div class="item-content">
									<input class="item-id" name="PRODUCT_IMAGE_ID[]" type="hidden" value="<{$product_image->ID}>"/>
									<input class="item-removed" name="PRODUCT_IMAGE_REMOVED[]" type="hidden" value="0"/>
									<table>
										<tr>
											<td width="8%"><{$smarty.const._LB_IMAGE|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td width="60%">
												<{if $product_image->IMAGE}>
													<img src="<{$smarty.const.PRODUCT_IMAGE_URL}>/thumb<{$product_image->IMAGE}>"><br/>
												<{/if}>
												<input name="PRODUCT_IMAGE[]" id="PRODUCT_IMAGE" type="file" size="60"/>
											</td>
										</tr>
										<tr>
											<td><{$smarty.const._LB_DISABLED|utf8_escape_html}>&nbsp;:&nbsp;</td>
											<td>
												<input class="display-checkbox" type="checkbox" value="1" <{if $product_image->DISABLED}>checked<{/if}>/>
												<input class="hide-checkbox" type="hidden" value="" name="PRODUCT_IMAGE_DISABLED[]" />
											</td>
										</tr>
									</table>
									<span class="remove-item link" style="padding-left: 2px;"><{$smarty.const._LB_REMOVE|utf8_escape_html}></span>
									<hr/>
								</div>
							<{/foreach}>
							</div>
							<span class="add-item link" style="padding-top:10px;">
								Add more image
							</span>
							<div class="sample-item" style="display:none;">
								<div class="item-content">
									<input class="item-id" name="PRODUCT_IMAGE_ID[]" type="hidden"/>
									<input class="item-removed" name="PRODUCT_IMAGE_REMOVED[]" type="hidden" value="0"/>
									<table>
										<tr>
											<td width="8%"><{$smarty.const._LB_IMAGE|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td width="60%">
												<input name="PRODUCT_IMAGE[]" id="PRODUCT_IMAGE" type="file" size="60"/>
											</td>
											<td>&nbsp;</td>
										</tr>
										<tr>
											<td><{$smarty.const._LB_DISABLED|utf8_escape_html}>&nbsp;:&nbsp;</td>
											<td>
												<input class="display-checkbox" type="checkbox" value="1" />
												<input class="hide-checkbox" type="hidden" value="" name="PRODUCT_IMAGE_DISABLED[]" />
											</td>
										</tr>
									</table>
									<span class="remove-item link" style="padding-left: 2px;"><{$smarty.const._LB_REMOVE|utf8_escape_html}></span>
									<hr/>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="spacer"></div>
				<div class="section">
					<div class="section_title"><{$smarty.const._LB_PRODUCT_DOWNLOAD|utf8_escape_html}></div>
					<div class="section_content">
						<div class="dynamic-list" style="padding-top: 10px;">
							<div class ="list-item">
							<{foreach key=key item=product_download from=$product->product_downloads}>
								<div class="item-content">
									<input class="item-id" name="PRODUCT_DOWNLOAD_ID[]" type="hidden" value="<{$product_download->ID}>"/>
									<input class="item-removed" name="PRODUCT_DOWNLOAD_REMOVED[]" type="hidden" value="0"/>
									<table>
										<tr>
											<td width="8%"><{$smarty.const._LB_NAME|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td width="60%"><input name="PRODUCT_DOWNLOAD_NAME[]" type="text" size="50" value="<{$product_download->NAME|utf8_escape_html}>" /></td>
											<td>&nbsp;</td>												
										</tr>
										<tr>
											<td><{$smarty.const._LB_TYPE|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td>
												<select class="select_type" name="PRODUCT_DOWNLOAD_IS_URL[]">
													<option value="0" <{if !$product_download->IS_URL}>selected<{/if}>><{$smarty.const._LB_FILE|utf8_escape_html}></option>
													<option value="1" <{if $product_download->IS_URL}>selected<{/if}>><{$smarty.const._LB_URL|utf8_escape_html}></option>
												</select>
											</td>
										</tr>
										<tr class="rw_file_upload">
											<td><{$smarty.const._LB_FILE|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td>
                                                <{$product_download->FILE|utf8_escape_html}>
												<input name="PRODUCT_DOWNLOAD_FILE[]" type="file" size="60"/>
											</td>
											<td>
												
											</td>
										</tr>
										<tr class="rw_url">
											<td><{$smarty.const._LB_URL|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td><input name="PRODUCT_DOWNLOAD_URL[]" type="text" size="50" value="<{if $product_download->IS_URL}><{$product_download->FILE|utf8_escape_html}><{/if}>" /></td>
										</tr>
										<tr>
											<td><{$smarty.const._LB_DISABLED|utf8_escape_html}>&nbsp;:&nbsp;</td>
											<td>
												<input class="display-checkbox" type="checkbox" value="1" <{if $product_download->DISABLED}>checked<{/if}> />
												<input class="hide-checkbox" type="hidden" value="" name="PRODUCT_DOWNLOAD_DISABLED[]" />
											</td>
										</tr>
									</table>
									<span class="remove-item link" style="padding-left: 2px;"><{$smarty.const._LB_REMOVE|utf8_escape_html}></span>
									<hr/>
								</div>
							<{/foreach}>
							</div>
							<span class="add-item link" style="padding-top:10px;">
								Add more file
							</span>
							<div class="sample-item" style="display:none;">
								<div class="item-content">
									<input class="item-id" name="PRODUCT_DOWNLOAD_ID[]" type="hidden"/>
									<input class="item-removed" name="PRODUCT_DOWNLOAD_REMOVED[]" type="hidden" value="0"/>
									<table>
										<tr>
											<td width="8%"><{$smarty.const._LB_NAME|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td width="60%"><input name="PRODUCT_DOWNLOAD_NAME[]" type="text" size="50" value="" /></td>
											<td>&nbsp;</td>												
										</tr>
										<tr>
											<td><{$smarty.const._LB_TYPE|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td>
												<select class="select_type" name="PRODUCT_DOWNLOAD_IS_URL[]">
													<option value="0"><{$smarty.const._LB_FILE|utf8_escape_html}></option>
													<option value="1"><{$smarty.const._LB_URL|utf8_escape_html}></option>
												</select>
											</td>
										</tr>
										<tr class="rw_file_upload">
											<td><{$smarty.const._LB_FILE|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td>
												<input name="PRODUCT_DOWNLOAD_FILE[]" type="file" size="60"/>
											</td>
										</tr>
										<tr class="rw_url">
											<td><{$smarty.const._LB_URL|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
											<td><input name="PRODUCT_DOWNLOAD_URL[]" type="text" size="50" value="" /></td>
										</tr>
										<tr>
											<td><{$smarty.const._LB_DISABLED|utf8_escape_html}>&nbsp;:&nbsp;</td>
											<td>
												<input class="display-checkbox" type="checkbox" value="1" />
												<input class="hide-checkbox" type="hidden" value="" name="PRODUCT_DOWNLOAD_DISABLED[]" />
											</td>
										</tr>
									</table>
									<span class="remove-item link" style="padding-left: 2px;"><{$smarty.const._LB_REMOVE|utf8_escape_html}></span>
									<hr/>
								</div>
							</div>
						</div>
					</div>
				</div>
				
				<div class="submit" align="right" style="padding-top:10px;">
					<a class="tribullet" href="<{$backlink}>"><{$smarty.const._LB_BACK}></a>
					<a class="tribullet" href="#" onclick="validate()"><{$smarty.const._LB_VALIDATE}></a>
				</div>
				</form>

			</div>
		</div>
<{include file="footer.html"}>
